<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","jo_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_jolist.php";
$FormName            = GetParam("FormName","");
$urut            = GetParam("urut","");
$debtur            = GetParam("debtur","");
$FileTemplate  = "ais_transaction_jolist_jobsliptruck_list.html";

$SubHeaderTitle = "Job Order Lookup";
$PageSize      = 10 ;
$HTMLGridList  = "JOGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$jo_code = GetParam("jo_code","");
$project_name = GetParam("project_name","");
$need_another_var = GetParam("need_another_var","");

//******* DATABASE FORM QUERY ***********


$SQL = " SELECT * FROM tbljoborder as a
         JOIN tblproject as b ON a.project_id = b.project_id
         JOIN tbldebtur as c ON a.debtur = c.CUCODE 
         JOIN tbljoactivity as d ON a.jo_id = d.jo_id
         JOIN tblservices as e ON d.services_id = e.services_id";


if ($jo_code)
    if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$jo_code."%'";
    else $WHERE .= " a.jo_code like '%".$jo_code."%'";

if ($project_name)
    if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$project_name."%'";
    else $WHERE .= " b.project_name like '%".$project_name."%'";

if (strlen($WHERE)) $WHERE = " WHERE e.services_code like '%LANDTRANSPORT%' AND ".$WHERE;
    else $WHERE = " WHERE e.services_code like '%LANDTRANSPORT%' ";

$SQL = $SQL.$WHERE." GROUP BY a.jo_code ORDER BY a.jo_id DESC ";



//echo "$SQL";

$WHERE="";

$SQLCount = " SELECT count(*) FROM tbljoborder as a
         JOIN tblproject as b ON a.project_id = b.project_id
         JOIN tbldebtur as c ON a.debtur = c.CUCODE 
         JOIN tbljoactivity as d ON a.jo_id = d.jo_id
         JOIN tblservices as e ON d.services_id = e.services_id";


if ($jo_code)
    if (strlen($WHERE)) $WHERE .= " AND "." a.jo_code like '%".$jo_code."%'";
    else $WHERE .= " a.jo_code like '%".$jo_code."%'";

if ($project_name)
    if (strlen($WHERE)) $WHERE .= " AND "." b.project_name like '%".$project_name."%'";
    else $WHERE .= " b.project_name like '%".$project_name."%'";

if (strlen($WHERE)) $WHERE = " WHERE e.services_code like '%LANDTRANSPORT%' AND ".$WHERE;
    else $WHERE = " WHERE e.services_code like '%LANDTRANSPORT%' ";

$SQLCount = $SQLCount.$WHERE." GROUP BY a.jo_code ORDER BY a.jo_id DESC ";



//Select Department
$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("tblstaff",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

?>
